<template>
<div>
  <el-card>
    <!-- 顶部面包屑标识与导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>酒店产品管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-row  style="margin-top: 10px">
      <el-button type="primary" @click="openAddNewDialog()"  style="margin-left: 10px">新增</el-button>
      <download-excel
          style="float: left"
          class="export-excel-wrapper"
          :data="roleData"
          type="xls"
          worksheet="worksheet"
          name="用户信息">
        <el-button type="primary">导出</el-button>
      </download-excel>
    </el-row>
    <el-table
        :data="hotelData"
        style="width: 100%">
      <el-table-column
          prop="id"
          label="酒店编号"
          width="80">
      </el-table-column>
      <el-table-column
          prop="name"
          label="酒店名称"
          width="280">
      </el-table-column>
      <el-table-column
          prop="categoryName"
          label="房间类型"
          width="100">
      </el-table-column>
      <el-table-column
          prop="price"
          label="房间价格"
          width="80px">
      </el-table-column>
      <el-table-column
          prop="address"
          label="酒店地址"
          width="250px">
      </el-table-column>
      <el-table-column
          prop="phone"
          label="酒店电话">
      </el-table-column>
      <el-table-column
          label="介绍">
        <template slot-scope="scope">
          <div class="info">
            {{scope.row.intro}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作"  width="200px"
                       align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="deleteRoom(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 修改数据的表单 -->
  <el-dialog title="编辑酒店数据" :visible.sync="editFormVisible">
    <el-form :model="editInfo"  label-width="120px">
      <el-form-item label="酒店名称" prop="name">
        <el-input v-model="editInfo.name"></el-input>
      </el-form-item>
      <el-form-item label="酒店地址" prop="location">
        <el-input v-model="editInfo.address"></el-input>
      </el-form-item>
      <el-form-item label="酒店电话" prop="phone">
        <el-input v-model="editInfo.phone"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="editInfo.price"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="editFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="edit">确 定</el-button>
    </div>
  </el-dialog>
  <!-- 新增数据的表单 -->
  <el-dialog title="新增酒店" :visible.sync="FormVisible">
    <el-form :model="addInfo"  label-width="120px">
      <el-form-item label="酒店名称" prop="name">
        <el-input v-model="addInfo.name"></el-input>
      </el-form-item>

      <el-form-item label="酒店地址" prop="location">
        <el-input v-model="addInfo.address"></el-input>
      </el-form-item>
      <el-form-item label="酒店电话" prop="phone">
        <el-input v-model="addInfo.phone"></el-input>
      </el-form-item>
      <el-form-item label="类型名称" prop="categoryName">
        <el-cascader
            v-model="addInfo.parentId"
            :options="categoryDataOptions"
            :props="{checkStrictly: true}"
            clearable="true"
            @change="handleChange"></el-cascader>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="addInfo.price"></el-input>
      </el-form-item>
      <el-form-item label="酒店介绍" prop="price">
        <el-input v-model="addInfo.intro"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="FormVisible = false">取 消</el-button>
      <el-button type="primary" @click="add">确 定</el-button>
    </div>
  </el-dialog>

  <el-pagination
      @current-change="changePage"
      layout="total, prev, pager, next"
      :hide-on-single-page="true"
      :page-size="pageSize"
      :current-page="currentPage"
      :total="total">
  </el-pagination>
</div>
</template>

<script>
export default {
  name: "HotelProduct",
  data:function (){
    return{
      hotelData: [],
      // 分页相关数据
      currentPage: this.$router.currentRoute.query.page ? parseInt(this.$router.currentRoute.query.page) : 1,
      pageSize: '',
      total:'',
      editFormVisible: false,
      FormVisible: false,
      categoryDataOptions:'',
      addInfo:{},
      editInfo:{},
    }
  },
  methods:{
    handleChange(value) {
      this.addInfo.categoryId=value[0];
    },
    openAddNewDialog() {
      this.loadCategoryTreeList();
      this.FormVisible = true
    },
    add(){
      console.log(this.addInfo)
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post("http://localhost:8080/v1/hotels/add",this.addInfo).then((response)=>{
        if(response.data.code==1){
          this.$message({
            message: '添加成功！',
            type: 'success'
          });
          this.FormVisible = false;
          this.loadHotelList();
        }
      })
    },
    // 弹出修改对话框
    openEditDialog(tableItem) {
      this.editFormVisible=true;
      this.editInfo=tableItem;
      console.log(tableItem)
    },
    edit(){
      console.log(this.editInfo)
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post("http://localhost:8080/v1/hotels/update",this.editInfo).then((response)=>{
        if(response.data.code==1){
          this.$message({
            message: '修改成功！',
            type: 'success'
          });
          this.editFormVisible = false;
          this.loadHotelList();
        }
      })
    },
    deleteRoom(id){
      console.log("==="+id);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post("http://localhost:8080/v1/hotels/"+id+"/delete").then((response)=>{
        let jsonResult = response.data;
        if (jsonResult.code == 1) {
          this.$message({
            message: '删除成功！',
            type: 'success'
          });
          this.loadHotelList();
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      })
    },
    changePage(page) {
      this.$router.replace('?page=' + page);
      this.loadHotelList();
    },
    loadHotelList(){
      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }
      let url = 'http://localhost:8080/v1/hotels/list?page='+page;
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get(url).then((response)=>{
        let jsonResult = response.data;
        if(response.data.code==1){
          this.hotelData = jsonResult.data.list;
          this.currentPage = jsonResult.data.currentPage;
          this.pageSize = jsonResult.data.pageSize;
          this.total = jsonResult.data.total;
        }
      })
    },

    loadCategoryTreeList() {
      let url = 'http://localhost:8080/v1/category/tree/?id=6';

      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.code === 1) {
          this.categoryDataOptions = jsonResult.data;
          console.log(jsonResult.data.list)
        }
      }).catch(err => {
        let message = '服务器响应错误！';
        this.$alert(message, '操作失败', {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
      });
    },
  },
  mounted() {
    this.loadHotelList();
  }
}
</script>

<style scoped>
.info{
  display: -webkit-box;/*作为弹性伸缩盒子模型显示*/
  -webkit-line-clamp: 3; /*显示的行数；如果要设置2行加...则设置为2*/
  overflow: hidden; /*超出的文本隐藏*/
  text-overflow: ellipsis; /* 溢出用省略号*/
  -webkit-box-orient: vertical;/*伸缩盒子的子元素排列：从上到下*/
}
</style>